﻿//功能描述：首页-菜单
using Dapper;
using Hong.Common.Encryption;
using Hong.IRepository.IRepository_SystemSettings.RBAC;
using Hong.Model.Dto.PesponseDto.ResponseModel.ResultModel;
using Hong.Model.SysDomain.Model_SystemSettings.RBAC;
using Hong.Model.SysDomain.Model_SystemSettings.ViewModel;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hong.Repository.Repository.Repository_Home
{
    public class MenuDal: Imenud
    {
        private readonly IDistributedCache _cache;
        //注入解密SQL连接字符串
        private readonly SecurityStatic _securityStatic;

        public MenuDal(IConfiguration configuration, IDistributedCache cache, SecurityStatic securityStatic)
        {
            _cache = cache;
            //注入解密SQL连接字符串
            _securityStatic=securityStatic;

        }
        

        #region 菜单加载
        public List<viewMenud> getmenus(string? Uid)
        {
            using (IDbConnection conn = new SqlConnection(_securityStatic.Securityinfo()))
            {
                var sql = $"select distinct m.MenudId,m.MenudName,m.MenudMenuId,m.MenudWebName,m.MenudGrade from MenudInfo m join Role_Menud r on m.MenudId = r.Mid join User_Role u on r.Rid = u.RoleId where 1 = 1";

                if (Uid != null)
                {
                    sql += $" and u.PersonnelId='{Uid}'";
                }
                var list = conn.Query<MenudInfo>(sql).ToList();
                return getmenud(list);
            }
        }
        #endregion

        #region 菜单全部显示
        public List<viewMenud> getmenusAll()
        {
            using (IDbConnection conn = new SqlConnection(_securityStatic.Securityinfo()))
            {
                var sql = $"select  MenudId,MenudName,MenudMenuId,MenudWebName,MenudGrade from MenudInfo ";

               var list = conn.Query<MenudInfo>(sql).ToList();
                return getmenud(list);
            }
        }
        #endregion

        #region 递归菜单
        public List<viewMenud> getmenud(List<MenudInfo> menuds, string MenudMenuId = "")
        {
            return menuds.Where(u => u.MenudMenuId == MenudMenuId).Select(u => new viewMenud
            {
                MenudId = u.MenudId,
                MenudName = u.MenudName,
                MenudWebName = u.MenudWebName,
                MenudMenuId = u.MenudMenuId,
                menudModels = getmenud(menuds, u.MenudId).Count == 0 ? null : getmenud(menuds, u.MenudId)
            }).ToList();
        }
        #endregion

        #region 分配菜单
        public ResponseModel<bool> AddRM(string RoleId, string Mid)
        {
            using (IDbConnection conn = new SqlConnection(_securityStatic.Securityinfo()))
            {
                var q = conn.Query($"select * from Role_Menud where Rid='{RoleId}'");
                if (q != null)
                {
                    conn.Execute($"delete from Role_Menud where Rid='{RoleId}'");
                }
                conn.Open();
                //事务
                using (var begin = conn.BeginTransaction())
                {
                    try
                    {
                        var menuIds = Mid.Split(',');
                        foreach (var item in menuIds)
                        {
                            conn.Execute($"insert into  Role_Menud values('{RoleId}','{item}')", null, begin);
                        }
                        begin.Commit();
                        ResponseModel<bool> res = new ResponseModel<bool>();
                        return res.Succeed(true);
                    }
                    catch (Exception)
                    {

                        throw;
                    }

                }

            }
        }
        #endregion

        #region 菜单回显
        public List<RoleMenud> getRM(string RoleId)
        {

            var list = new List<RoleMenud>();
            using (IDbConnection conn = new SqlConnection(_securityStatic.Securityinfo()))
            {
                list = conn.Query<RoleMenud>($"select * from Role_Menud where Rid='{RoleId}'").ToList();
            }
            return list;
        }
        #endregion

    }
}
